import { setGlobal } from '@swda/utils'

//-----------------------------------------------------------------------------
// SWDA_Window_Component_Scroll
//
// The superclass of window_base
// * 【使用方法】
// * 1. visible = true显示
//   2.onOut = true 在窗口外面显示，否则在窗口内部显示

class Window_Component_ScrollBar extends Window_Base {
    constructor(props) {
        super(props)

        this.visible = false;
        this.direction = 'right';
        this.onOut = false;

        this.offsetX = 5;
        this.offsetY = 0;

        this._cursor = null;
        this._cursorWidth = 30;
        this._cursorPaddingTop = 32;
        this._lastScrollY = -1;
        this._touching = false;

        this._currentPressDate = null;
        this._onPressDate = null;

        this.initAllParts();
    }

    get cursor() {
        return this._cursor;
    }

    get cursorWidth() {
        return this._cursorWidth
    }

    get scrollHeight() {
        //return this.parent.contentsHeight() - this._bgTop.height - this._bgBottom.height;
        return this.parent.contentsHeight() - this._bgTop.height - this._bgBottom.height - this._cursor.height; // 老饕：减去红点光标的高度(14)
    }

    get cursorHeight() {
        if (!this.parent) return 1;
        //const scrollRows = this.parent.remaindRows() + 1;
        const scrollRows = this.parent.remaindRows() ; //老饕：不确定，似乎多算了一行，改完看起来很完美
        return Number((this.scrollHeight / scrollRows).toFixed(2));
    }

    setOffset(x = 0, y = 0) {
        this.offsetX = x
        this.offsetY = y
    }

    isOpenAndActive() {
        const parentActive = this.parent && this.parent.active
        return this.isOpen() && this.active && parentActive;
    };

    isTouchedInsideFrame() {
        const x = this.canvasToLocalX(TouchInput.x);
        const y = this.canvasToLocalY(TouchInput.y);
        return x >= 0 && y >= 0 && x < this.width && y < this.height;
    };

    initAllParts() {
        // block - 滑块
        this._cursor = new Sprite()
        this._cursor_bitmap_default = ImageManager.loadBitmap('img/system/UI/', 'MenuSlider_block')
        this._cursor.bitmap = this._cursor_bitmap_default
        this._cursor.bitmap.addLoadListener(() => {
            this._cursor.x = (this._cursorWidth - this._cursor.width) * 0.5;
            this._cursor.y = 0;
        })

        // top - 上方箭头
        this._bgTop = new Sprite();
        this._bgTop_bitmap_default = ImageManager.loadBitmap('img/system/UI/', 'MenuSlider_top')
        this._bgTop_bitmap_onTouch = ImageManager.loadBitmap('img/system/UI/', 'MenuSlider_top_onTouch')
        this._bgTop.bitmap = this._bgTop_bitmap_default

        // center - 中部背景条
        this._bgCenter = new TilingSprite();
        this._bgCenter.bitmap = ImageManager.loadBitmap('img/system/UI/', 'MenuSlider_center')

        // bottom - 下方箭头
        this._bgBottom = new Sprite()
        this._bgBottom_bitmap_default = ImageManager.loadBitmap('img/system/UI/', 'MenuSlider_bottom')
        this._bgBottom_bitmap_onTouch = ImageManager.loadBitmap('img/system/UI/', 'MenuSlider_bottom_onTouch')
        this._bgBottom.bitmap = this._bgBottom_bitmap_default;

        // addAll
        this.addChild(this._bgTop);
        this.addChild(this._bgCenter);
        this.addChild(this._bgBottom);
        this.addChild(this._cursor)

    }

    updateVisible() {
        if (!this.parent) return;
        if (this.parent.maxRows() <= 0 || this.parent.visibleRows() <= 0) { this.visible = false; return }

        const nextVisible = this.parent.maxRows() > this.parent.visibleRows();
        if (nextVisible !== this.visible) { this.visible = nextVisible; return }
    }

    updateCursorPosition() {
        if (this._lastScrollY === this.parent._scrollY) return;

        const topRow = this.parent.topRow();
        this.cursor.y = topRow * this.cursorHeight + this._bgTop.height;
        this._lastScrollY = this.parent._scrollY;
    }

    drawBar() {
        if (!this.parent) return;

        // set x and y
        this.x = this.parent.width - this.cursorWidth + this.offsetX;
        if (this.onOut) this.x = this.parent.width + this.offsetX;

        this.y = this.parent.standardPadding() + this.offsetY;
        this.width = this._bgTop.width;
        this.height = this.parent.contentsHeight()

        // setTop's Position
        this._bgTop.y = 0;

        // setCenter's Position
        this._bgCenter.y = this._bgTop.height;
        this._bgCenter.move(0, this._bgTop.height, this.cursorWidth, this.parent.contentsHeight() - this._bgTop.height - this._bgBottom.height)

        // setBottom's Position
        this._bgBottom.y = this._bgCenter.height + this._bgCenter.y;
    }

    processTouch() {
        if (!this.isOpenAndActive()) {
            this._touching = false;
            return;
        }

        if (TouchInput.isTriggered() && this.isTouchedInsideFrame()) {
            this._touching = true;
            this.onTouch(true);
        }
        if (this._touching) {
            if (TouchInput.isPressed()) {
                this.onTouch(false);
            } else {
                this._touching = false;
                this.onTouchUp()
            }
        }

    }

    processMove() {
        if (this.isOpenAndActive()) {
        }
    }

    onTouch(triggered) {
        const y = this.canvasToLocalY(TouchInput.y);
        this._currentPressDate = Date.now();

        if (y > 0 && y <= this._bgTop.height) {
            this._bgTop.bitmap = this._bgTop_bitmap_onTouch

            if (!this._onPressDate || this._currentPressDate - this._onPressDate > 300) {
                this.parent.setTopRow(this.parent.topRow() - this.parent.visibleRows())
                if (this._lastRow === this.parent.topRow()) return;
                SoundManager.playCursor();
                this._onPressDate = Date.now();
                this._lastRow = this.parent.topRow()
            }
        }

        if (y > this._bgCenter.y && y <= this._bgCenter.y + this._bgCenter.height) {
            const row = Math.floor((y - this._bgTop.height) / this.cursorHeight)
            this.parent.setTopRow(row)
        }

        if (y > this._bgBottom.y && y < this._bgBottom.y + this._bgBottom.height) {
            this._bgBottom.bitmap = this._bgBottom_bitmap_onTouch

            if (!this._onPressDate || this._currentPressDate - this._onPressDate > 300) {
                this.parent.setTopRow(this.parent.topRow() + this.parent.visibleRows())
                if (this._lastRow === this.parent.topRow()) return;
                SoundManager.playCursor();
                this._onPressDate = Date.now();
                this._lastRow = this.parent.topRow()
            }
        }

    }

    onTouchUp() {
        this._bgTop.bitmap = this._bgTop_bitmap_default;
        this._bgBottom.bitmap = this._bgBottom_bitmap_default;
        this._onPressDate = null;
    }

    update() {
        if (!this.parent) return;
        this.updateVisible();
        if (!this.visible) return;
        this.processTouch();
    }
}

setGlobal('Window_Component_ScrollBar', Window_Component_ScrollBar)

const Window_Selectable_Init = Window_Selectable.prototype.initialize
const Window_Selectable_DrawAllItems = Window_Selectable.prototype.drawAllItems
const Window_Selectable_updateCursor = Window_Selectable.prototype.updateCursor
Window_Selectable.prototype.initialize = function (x, y, width, height) {
    Window_Selectable_Init.apply(this, arguments)
    this.createScrollBar()
}

Window_Selectable.prototype.createScrollBar = function () {
    this._scrollbar = new Window_Component_ScrollBar()
    this.addChild(this._scrollbar)
    this._scrollbar.drawBar()
}

Window_Selectable.prototype.drawAllItems = function () {
    Window_Selectable_DrawAllItems.apply(this, arguments)
    this._scrollbar && this._scrollbar.drawBar()
}

Window_Selectable.prototype.updateCursor = function(){
    Window_Selectable_updateCursor.apply(this,arguments)
    this._scrollbar && this._scrollbar.updateCursorPosition()
}

Window_Selectable.prototype.visibleRows = function () {
    return Math.floor(this.contentsHeight() / this.itemHeight());
}

Window_Selectable.prototype.remaindRows = function () {
    return this.maxRows() - this.visibleRows()
}